<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/8
 * Time: 15:01
 */

namespace app\api\controller\v1;




use app\api\controller\BaseController;
use app\api\service\Token;
use app\api\service\WorkWord;
use app\api\validate\WordParameter;
use app\lib\exception\SuccessMessage;
use app\lib\exception\WorkWordException;

class Word extends BaseController
{
    /**
     * 添加分类
     * @return \think\response\Json
     * @throws WorkWordException
     */
    public function addClassify(){
        $uid = Token::getCurrentUid();
        $dataArray = input('post.');
        $service = new WorkWord();
        $result =  $service->addWordClassify($dataArray,$uid);
        if (empty($result)){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'分类添加失败',
                'err_code'=>95000
            ]);
        }
        return json(new SuccessMessage());
    }
    /**
 * 删除分类
 * @param $id
 * @return \think\response\Json
 * @throws WorkWordException
 */
    public function deleteClassify($id){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->getWordList($uid,$id);
        if (!$result->isEmpty()) {
            throw new WorkWordException([
                'code'=>201,
                'msg'=>'已有类容，不能删除',
                'err_code'=>950001
            ]);
        }
        $result =  $service->deleteClassify($id);
        if (!$result){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'删除分类失败',
                'err_code'=>950001
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 更新分类
     * @return \think\response\Json
     * @throws WorkWordException
     */
    public function updateClassify(){
        $uid = Token::getCurrentUid();
        $dataArray = input('post.');
        $service = new WorkWord();
        $result =  $service->updateClassify($dataArray);
        if (!$result){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'更新分类失败啊',
                'err_code'=>950002
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 获取分裂列表
     * @return \think\response\Json
     * @throws WorkWordException
     */
    public function getClassify(){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->getClassify($uid);
        if (!$result){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'获取分诶列表失败',
                'err_code'=>950003
            ]);
        }
        return $result;
    }

    /**
     * 添加话术
     * @throws WorkWordException
     */
    public function addWord(){
        (new WordParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $data = input('post.');
        $service = new WorkWord();
        $result = $service->addWord($data,$uid);
        if (!$result){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'添加话术失败',
                'err_code'=>950004
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 获取话术类容
     * @param $id
     * @return null|static
     * @throws WorkWordException
     */
    public function getWord($id){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->getWord($id);
        if (!$result){
            throw new WorkWordException([
                'code'=>201,
                'msg'=>'获取话术内容失败',
                'err_code'=>950008
            ]);
        }
        return $result;
    }

    /**
     * 更新话术库失败
     * @return \think\response\Json
     * @throws WorkWordException
     */
    public function updateWord(){
        (new WordParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $data = input('post.');
        $service = new WorkWord();
        $result = $service->updateWord($data,$uid);
        if (!$result){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'更新话术库失败',
                'err_code'=>950007
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * s删除话术
     * @param $id
     * @return \think\response\Json
     * @throws WorkWordException
     */
    public function  deleteWord($id){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->deleteWord($id);
        if (!$result){
            throw new WorkWordException([
                'code'=>400,
                'msg'=>'删除话术失败',
                'err_code'=>950006
            ]);
        }
        return json(new SuccessMessage());
    }

    /**
     * 获取话术列表
     * @return false|\PDOStatement|string|\think\Collection
     * @throws WorkWordException
     */
    public function  getWordList($c_id){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->getWordList($uid,$c_id);
        if ($result->isEmpty()){
            throw new WorkWordException([
                'code'=>201,
                'msg'=>'获取话术列表失败',
                'err_code'=>950004
            ]);
        }
        return $result;
    }


    /**
     * 根据用户id获取话术分类列表
     * @return \think\response\Json
     */
    public function getWordCategoryByUser(){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->getClassifyBuUid($uid);
        if (!$result->isEmpty()){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }


    /**
     * 添加用户
     * @return \think\response\Json
     */
    public function addUserWord(){
        (new WordParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $dataArray = input('post.');
        $service = new WorkWord();
        $result =  $service->addUserWord($dataArray,$uid);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 更新自定义话术
     * @return \think\response\Json
     */
    public function updateUserWord(){
        (new WordParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $dataArray = input('post.');
        $service = new WorkWord();
        $result =  $service->updateUserWord($dataArray,$uid);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * s删除话术
     * @param $id
     * @return \think\response\Json
     * @throws WorkWordException
     */
    public function  deleteUserWord($id){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result = $service->deleteWord($id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取自定义话术列表
     * @return \think\response\Json
     */
    public function geteUserWordList(){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result =  $service->getUserWordList($uid);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     *根据用户ID获取分类
     * @return \think\response\Json
     */
    public function getClassifyByUid(){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result =  $service->getClassifyBuUid($uid);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }
    /**
     *根据分类获取话术列表
     * @return \think\response\Json
     */
    public function getWordByCategory($categoryId){
        $uid = Token::getCurrentUid();
        $service = new WorkWord();
        $result =  $service->getWordByCategory($uid,$categoryId);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json($data);
    }

}